package com.framework.util.images;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import org.apache.commons.httpclient.HttpException;
import org.springframework.beans.factory.annotation.Value;

import com.framework.bdt.FileBdt;
import com.framework.bdt.enumClass.FileType;
import com.framework.bdt.enumClass.ImageType;
import com.framework.util.AbstractFileServerUtility;
import com.framework.util.CommonUtil;

public class ImageFileUtility  extends AbstractFileServerUtility{

	private final String thumbnail = "-thumbnail";					// 파일 이름 뒤 썸네일 문자
	private final String watermark = "-watermark";					// 파일 이름 뒤 워터마크 문자
	private static final FileType fileType = FileType.IMAGE;			// 파일 타입.

	@Value("${tempFolder}")
	private static String tempFolder;

	@Value("${watermarkImage}")
	private static String watermarkImage;

	/**
	 * 이미지파일 서버 파일 올리기
	 *
	 * @param fileBdt
	 * @param imageType
	 * @param destWidth
	 * @param destHeight
	 * @return
	 * @throws HttpException
	 * @throws IOException
	 */
	public boolean runImageUpload(FileBdt fileBdt, ImageType imageType, int destWidth, int destHeight) throws IOException {
		ImageType type = imageType;
		if(!CommonUtil.getFileType(fileType, fileBdt.getFile())){
			throw new IOException("FIleType is not IMAGE");
		}
		List<FileBdt> fileBdts = null;
		switch (type) {
		case NOMAL:
			return runFileServerUpload(fileType, fileBdt);
		case THUMBNAIL:
			fileBdts = new ArrayList<FileBdt>();
			fileBdts.add(fileBdt);
			fileBdts.add(imageThumbnail(fileBdt, destWidth, destHeight));
			return runFileServerUpload(fileType, fileBdts);
		case THUMBNAIL_ONTY:
			return runFileServerUpload(fileType, imageThumbnail(fileBdt, destWidth, destHeight));
		case WATERMARK:
			fileBdts = new ArrayList<FileBdt>();
			fileBdts.add(fileBdt);
			fileBdts.add(imageWatermark(fileBdt));
			return runFileServerUpload(fileType, fileBdts);
		case WATERMARK_ONTY:
			return runFileServerUpload(fileType, imageWatermark(fileBdt));
		case THUMBNAIL_AND_WATERMARK:
			fileBdts = new ArrayList<FileBdt>();
			fileBdts.add(fileBdt);
			fileBdts.add(imageThumbnail(fileBdt, destWidth, destHeight));
			fileBdts.add(imageWatermark(fileBdt));
			return runFileServerUpload(fileType, fileBdts);
		default:
			return false;
		}
	}

	/**
	 * 썸네일 만들기
	 *
	 * @param fileBdt
	 * @param destWidth
	 * @param destHeight
	 * @return
	 * @throws IOException
	 */
	private FileBdt imageThumbnail(FileBdt fileBdt, int destWidth, int destHeight) throws IOException {
		File tmpFile = new File(tempFolder + fileBdt.getOriginalFileName() + thumbnail + fileBdt.getFileExtension());
		Thumbnails.of(fileBdt.getFile()).size(destWidth, destHeight).toFile(tmpFile);
		return new FileBdt(tmpFile);
	}

	/**
	 * 이미지에 워터마크 찍기
	 *
	 * @param fileBdt
	 * @return
	 * @throws IOException
	 */
	private FileBdt imageWatermark(FileBdt fileBdt) throws IOException {
		File tmpFile = new File(tempFolder + fileBdt.getOriginalFileName() + watermark + fileBdt.getFileExtension());
		Thumbnails.of(fileBdt.getFile()).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(watermarkImage)), 0.5f).toFile(tmpFile);
		return new FileBdt(tmpFile);
	}
}
